package com.ikas.ai.server.module.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.error.BusinessException;
import com.ikas.ai.server.module.system.dao.DictDAO;
import com.ikas.ai.server.module.system.model.Dict;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SuppressWarnings("all")
@Service
public class DictService extends ServiceImpl<DictDAO, Dict> {

    @Autowired
    private DictDAO dictDAO;

    /**
     * 分页查询
     *
     * @param type
     * @param code
     * @param name
     * @return
     */
    public List<Dict> pageQueryDict(String type, String code, String name) {
        LambdaQueryChainWrapper<Dict> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(dictDAO);
        List<Dict> list = lambdaQueryChainWrapper.eq(StringUtils.isNotBlank(type), Dict::getType, type)
                .eq(StringUtils.isNotBlank(code), Dict::getCode, code)
                .like(StringUtils.isNotBlank(name), Dict::getName, name)
                .list();
        return list;
    }

    public void saveDict(Dict param) {
        String type = param.getType();
        String code = param.getCode();
        List<Dict> list = getConfigParams(type, code);
        if (list != null && list.size() > 0) {
            list.forEach(o -> {
                if (o.getCode().equalsIgnoreCase(code)) {
                    throw new BusinessException("该参数编码已经存在");
                }
            });
        }
        dictDAO.insert(param);
    }

    public void updateDict(Dict param) {
        int id = param.getId();
        String type = param.getType();
        String code = param.getCode();
        List<Dict> list = getConfigParams(type, code);
        if (list != null && list.size() > 0) {
            list.forEach(o -> {
                if (o.getId().intValue() != id && o.getCode().equalsIgnoreCase(code)) {
                    throw new BusinessException("该参数编码已经存在");
                }
            });
        }
        dictDAO.updateById(param);
    }

    /**
     * 删除字典信息
     *
     * @param iArr
     */
    public void deleteDict(List<Integer> iArr) {
        if (CollectionUtils.isNotEmpty(iArr)) {
            dictDAO.deleteBatchIds(iArr);
        }
    }

    private List<Dict> getConfigParams(String type, String code) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dict::getType, type);
        queryWrapper.eq(Dict::getCode, code);
        List<Dict> list = dictDAO.selectList(queryWrapper);
        return list;
    }

    /**
     * @param type
     * @return java.util.List<com.ikas.pe.server.module.system.model.Dict>
     * @description 通过type查询所有字典列表
     * @author deng.baohui
     * @date 2021/11/30 18:37
     */
    public List<Dict> dictListByType(String type) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dict::getType, type);
        queryWrapper.orderByDesc(Dict::getId);
        List<Dict> list = dictDAO.selectList(queryWrapper);
        return list;
    }

    /**
     * @param types
     * @return {@link Map<String,List<Dict>>}
     * @description 根据多个参数类型查询对应的字典信息
     * @date 2021/12/9
     */
    public Map<String, List<Dict>> dictListByTypes(String[] types) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        List<String> asList = Arrays.asList(types);
        //查询所有字典信息
        if (!asList.contains("all") || !asList.contains("ALL")) {
            queryWrapper.in(Dict::getType, types);
        }
        List<Dict> dictList = dictDAO.selectList(queryWrapper);
        Map<String, List<Dict>> map = dictList.stream().collect(Collectors.groupingBy(Dict::getType));
        return map;
    }

    /**
     * 获取多个字典集合--多参数
     * @param types
     * @return
     */
    public List<Dict> dictListByType(String[] types) {
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        List<String> asList = Arrays.asList(types);
        //查询所有字典信息
        if (!asList.contains("all") || !asList.contains("ALL")) {
            queryWrapper.in(Dict::getType, types);
        }
        List<Dict> dictList = dictDAO.selectList(queryWrapper);
        return dictList;
    }

    /**
     * 根据字典类型和编码查询具体的字典信息
     * @param type
     * @param code
     * @return
     */
    public Dict getDictInfo(String type, String code){
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dict::getType,type).eq(Dict::getCode,code);
        Dict dict = dictDAO.selectOne(queryWrapper);
        return dict;
    }

}
